package io.netty.groupchat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 群聊客户端处理
 *
 * @author Verlet
 * @date 2023-02-05 00:42
 */
@Slf4j
public class GroupChatClinetHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.print("\b\b\b");
        String[] split = msg.split("==>");
        if(split.length>2){
            String  hostname =split[0].trim();
            msg = msg.split("==>")[1].trim();
            log.info(" {} 发送的数据 ==> {}", hostname, msg);
        }else{
            log.info(msg);
        }
        System.out.print(">> ");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.print("\b\b\b");
        log.error(cause.getMessage());
        ctx.channel().close();
        System.out.print(">> ");
    }
}
